package com.example.demo.leetcode.classics150;

import java.util.List;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _139单词拆分
 * @description
 * @date 2023/08/26 14:10
 * <p>
 * ******************************************************
 */
public class _139单词拆分 {

    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            boolean res = false;
            for (String word : wordDict) {
                if (res) {
                    break;
                }
                if (s.substring(0, i).endsWith(word)) {
                    int wl = word.length();
                    if (wl <= i) {
                        res = dp[i - wl];
                    }
                }
            }
            dp[i] = res;
        }
        return dp[s.length()];
    }

}
